var Config = {
    //添加必要的请求参数
    setFullParameter: function (params) {
        if (typeof $.cookie("token") === 'undefined' || $.cookie("token") == '') {
            if (typeof toLogin === 'function') {
                //toLogin();
            }
        }
        if ($.isArray(params)) {
            //设置请求随机数
            params.push({"name": "_r", "value": Math.random()});
            params.push({"name": "token", "value": $.cookie("token")});
            params.push({"name": "client_id", "value": "PCCLIENT"});
            params.push({"name": "client_secret", "value": "!sdfioaflksdxcijvn"});
            params.push({"name": "channel", "value": "pc"});
            params.push({"name": "version", "value": "v1.0"});
            params.push({"name": "request_hash", "value": location.hash});
        } else {
            //设置请求随机数
            params._r = Math.random();
            params.token = $.cookie("token");
            params.client_id = "PCCLIENT";
            params.client_secret = "!sdfioaflksdxcijvn";
            params.channel = "pc";
            params.version = "v1.0";
            params.request_hash = location.hash;
        }
        return params;
    }
};

//web 服务器地址获取
Config.webRoot = function () {
    var js = document.scripts;
    var jsroot = '';
    for (var i = js.length; i > 0; i--) {
        jsroot = js[i - 1].src;
        if (jsroot.indexOf("js/common/ajax_common.js") > -1) {
            jsroot = jsroot.substring(0, jsroot.indexOf("js/common/ajax_common.js"));
            break;
        } else if (jsroot.indexOf("js/plugins/jquery-easyui-1.3.6/jquery.easyui.min.js") > -1) {
            jsroot = jsroot.substring(0, jsroot.indexOf("js/plugins/jquery-easyui-1.3.6/jquery.easyui.min.js"));
            break;
        }
    }
    return jsroot;
};
// 设置_apiRoot地址
Config.apiRoot = function () {
    // 设置_apiRoot地址
    var _apiRoot = Config.webRoot().substring(0, Config.webRoot().length - 1);
    return _apiRoot.substring(0, _apiRoot.lastIndexOf('/'));
};


//注册到window
window.setFullParameter = Config.setFullParameter;

//初始化定义系统配置
Config.sysConfig = {};

//获取系统配置
/*$.ajax({
    //地址格式
    url: Config.apiRoot()+'/api/v1/sysconfig',
    type: 'GET',
    dataType: 'jsonp',
    cache: 'false',
    async:false,
    data:  {r:Math.random()},
    success: function(data) {
        if(data.code=='200' && data.businessMsg.businessCode=='0' && typeof data.data === 'object'){
            Config.sysConfig=data.data;
        }
    }
});*/

//服务器图片
Config.imgRoot = function () {
    var imgrs = [];
    if (typeof Config.sysConfig.imgRoot === 'string') {
        imgrs = Config.sysConfig.imgRoot.split(',');
    }
    return imgrs[Math.floor(Math.random() * imgrs.length)];
};
//跳转到登录
var toLogin = function () {
    if (window.location.href.indexOf(Config.webRoot() + "html/login.html") < 0) {
        top.location.href = Config.webRoot() + "html/login.html?url=" + encodeURIComponent(window.location.href);
    }
};

function _common_request(action, params, care, callback, $jump401, $gtype, $complete) {
    //如果参数为空设置为空对象
    if (typeof params === 'undefined' || typeof params != 'object') {
        params = {}
    }
    if ($gtype == undefined || $gtype == '') {
        $gtype = "get";
    }
    var ajaxurl = Config.apiRoot() + action;
    $.ajax({
        //地址格式
        url: ajaxurl,
        type: $gtype,
        dataType: 'jsonp',
//			jsonp: "callback",
//			contentType: "application/json;charset=UTF-8",
        cache: 'false',
        data: setFullParameter(params),
        success: function (jsonstr) {
            if (callback != null) {
                if (jsonstr.code == "200") //业务异常
                {
                    callback(jsonstr.data, jsonstr.code, jsonstr.businessMsg);
                } else if (jsonstr.code == "400") {
                    //是否关心异常码
                    if (care) {
                        callback(jsonstr.data, jsonstr.code, jsonstr.businessMsg);
                    } else {
                        layer.closeAll('loading'); //关闭加载层
                        layer.msg(jsonstr.businessMsg.businessNote);
                    }
                } else if (jsonstr.code == '401') {//未登录
                    //loginJs.loadHtml();
                    if (typeof $jump401 === 'function') {
                        $jump401($resultdata.code, $resultdata.businessMsg);
                    } else {
                        //toLogin();
                    }
                } else {
                    if (jsonstr.businessMsg.businessNote != null && jsonstr.businessMsg.businessNote != '') {
                        layer.closeAll('loading'); //关闭加载层
                        layer.msg(jsonstr.businessMsg.businessNote);
                    } else {
                        layer.closeAll('loading'); //关闭加载层
                        layer.msg(JSON.stringify(jsonstr.businessMsg));
                    }

                }
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.info(jqXHR.responseText, "chucuo");
            //TODO:隐藏弹出框
            //TODO:提示消息
            if (jqXHR.readyState === 0 && jqXHR.status === 0) {
            } else {
                //_log(JSON.stringify(jqXHR) + "," + textStatus + "," + errorThrown);
                _log("系统异常");
                layer.closeAll('loading'); //关闭加载层
            }
            //						alert(jqXHR.responseText);
            //						alert("error");
        },
        complete: function () {
            console.info("完成");
            if (typeof $complete === 'function') {
                $complete();
            }
        }
    });
}

function getApiRoot() {
    return Config.apiRoot();
}

function setFullParameter(params) {
    return Config.setFullParameter(params);
}

//获取url参数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}


function layerConfirm(content, callBack) {
    layer.confirm(content, {
        icon: "fa-exclamation-circle",
        title: "系统提示",
        btn: ['确认', '取消'],
        btnclass: ['btn btn-primary', 'btn btn-danger'],
    }, function (index) {
        callBack(true);
        layer.close(index);
    }, function () {
        callBack(false);
    });
}